﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using System.Xml;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Controls;

using PlanningGIS.Util.Convertor;
using PlanningGIS.ArcGIS;
using PlanningGIS.Util.Log;
using PlanningGIS.Engine.Controls;

namespace PlanningGIS.DataCheck.model
{
    /// <summary>
    /// 河湖洲江岛名称2检查（对于河湖洲江岛，必须在一个水系面内部，检查他的名称2属性是否与所在所在水系面的名称属性一致）
    /// </summary>
    public class HydIslandName2Check : Check
    {
        public int Check(string errType, IFeatureLayer pFeatLyr, IGeometry pGeometry, string whereClause,
            string fcodename, string fname1name, string fname2name)
        {
            IFeatureClass pFeatCls = pFeatLyr.FeatureClass;
            if (pFeatCls.ShapeType != esriGeometryType.esriGeometryPolygon)
            {
               LogHelper.WriteErrLog("[" + pFeatLyr.Name + "]图层不是面状图层，检查失败！");
                return -1;
            }

            IFeatureSelection pFeatureSelecton = pFeatLyr as IFeatureSelection;

            int idxfcode = pFeatCls.FindField(fcodename);
            if (idxfcode == -1)
            {
               LogHelper.WriteErrLog("[" + pFeatLyr.Name + "]图层中未找到分类代码[" + fcodename + "]字段，检查失败！");
                return -1;
            }

            int idxfname1 = pFeatCls.FindField(fname1name);
            if (idxfname1 == -1)
            {
               LogHelper.WriteErrLog("[" + pFeatLyr.Name + "]图层中未找到名称[" + fname1name + "]字段，检查失败！");
                return -1;
            }

            int idxfname2 = pFeatCls.FindField(fname2name);
            if (idxfname2 == -1)
            {
               LogHelper.WriteErrLog("[" + pFeatLyr.Name + "]图层中未找到名称2[" + fname2name + "]字段，检查失败！");
                return -1;
            }

            ISpatialFilter pSpatialFileter = new SpatialFilterClass();
            if (pGeometry != null) pSpatialFileter.Geometry = pGeometry;

            string sql = fcodename + " in('260500500')";
            pSpatialFileter.WhereClause = whereClause == "" ? sql : whereClause + " and " + sql;

            pSpatialFileter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
            pSpatialFileter.GeometryField = pFeatCls.ShapeFieldName;

            showFakePro(true);
            LogHelper.WriteLog("正在计算需要检查的要素数目...");

            int max = FeatureClassHelper.GetFeatureCountFromLayer(pFeatLyr, pSpatialFileter);// pFeatCls.FeatureCount(pSpatialFileter);
           

            LogHelper.WriteLog("计算完毕。共有" + max + "个要素参与检查。");
            showFakePro(false);

            if (max == 0)
            {
                LogHelper.WriteLog("没有满足检查要求的要素存在。");
                return -1;
            }

            setProcessBarMax(max);

            IFeatureCursor pFeatCur;
            pFeatCur = pFeatLyr.Search(pSpatialFileter, false);

            IFeature pFeature;

            int count = 0;
            int errCount = 0;

            while ((pFeature = pFeatCur.NextFeature()) != null)
            {
                if (IsStop()) break;

                setProcessBarCurrent(count);
                count++;

                string fname2value = StringHelper.ConvertWRTNull(pFeature.get_Value(idxfname2));
                if (fname2value == "0" || fname2value == " ") fname2value = "";

                ISelectionSet pSelectionSet = CursorHelper.GetSelset(pFeatureSelecton, pFeature.ShapeCopy, pFeatCls.OIDFieldName + " <> " + pFeature.OID, esriSpatialRelEnum.esriSpatialRelIntersects);

                if (pSelectionSet.Count == 0)
                {
                    RecordListHelper.AddRecordItem(errType, pFeatLyr, null, pFeature, null, pFeature.ShapeCopy, "找不到与该岛相接的水系面");
                    errCount++;
                    continue;
                }

                IEnumIDs pIDs = pSelectionSet.IDs;
                pIDs.Reset();
                int id;

                ITopologicalOperator2 pTop = pFeature.ShapeCopy as ITopologicalOperator2;
                pTop.IsKnownSimple_2 = false;
                pTop.Simplify();

                while ((id = pIDs.Next()) != -1)
                {
                    IFeature pFeature2 = pFeatCls.GetFeature(id);
                    string fname1value = StringHelper.ConvertWRTNull(pFeature2.get_Value(idxfname1));
                    if (fname1value == "0" || fname1value == " ") fname1value = "";

                    if (fname1value != fname2value)
                    {
                        IGeometry pGeomRst = pTop.Intersect(pFeature2.ShapeCopy, esriGeometryDimension.esriGeometry2Dimension);
                        if (pGeomRst == null || pGeomRst.IsEmpty)
                            pGeomRst = pTop.Intersect(pFeature2.ShapeCopy, esriGeometryDimension.esriGeometry1Dimension);

                        if (pGeomRst == null || pGeomRst.IsEmpty) 
                            pGeomRst = pFeature.ShapeCopy;

                        RecordListHelper.AddRecordItem(errType, pFeatLyr, null, pFeature, pFeature2, pGeomRst, "");
                        errCount++;
                    }

                }

                pFeatureSelecton.Clear();

            }

            finishPro.Invoke();

            DataCheckUtility.ReleaseComObject(pFeature);
            DataCheckUtility.ReleaseComObject(pFeatCur);

            LogHelper.WriteLog("河湖洲江岛名称2检查结束。共发现" + errCount + "处错误！\n");

            return errCount;
        }
    }
}
